<template>
  <div>
    <el-card class="box-card">
      <div
        slot="header"
        class="clearfix"
      >
        <span>修改密码</span>
      </div>
      <div class="content">
        <el-form
          :model="modifyPasswordFrom"
          :rules="rules"
          ref="modifyPasswordFrom"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item
            label="原密码"
            prop="oldPassword"
          >
            <el-input
              type="password"
              v-model="modifyPasswordFrom.oldPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="新密码"
            prop="newPassword"
          >
            <el-input
              :type="passwordInputType"
              v-model="modifyPasswordFrom.newPassword"
              autocomplete="off"
            >
              <i
                slot="suffix"
                @click="changeType"
                class="iconfont"
                :class="{ 'icon-eye-close': passwordInputType === 'password', 'icon-eye-open': passwordInputType === 'text' }"
              ></i>
            </el-input>
          </el-form-item>
          <el-form-item
            label="确认新密码"
            prop="confirmPassword"
          >
            <el-input
              :type="passwordInputType2"
              v-model="modifyPasswordFrom.confirmPassword"
              autocomplete="off"
            ><i
                slot="suffix"
                @click="changeType2"
                class="iconfont"
                :class="{ 'icon-eye-close': passwordInputType2 === 'password', 'icon-eye-open': passwordInputType2 === 'text' }"
              ></i></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm"
            >提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
// import { validateOldPwd, updatePwd } from '@/api/user'
export default {
  data() {
    var validateOldPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入旧密码"));
      } else if (!/^[a-zA-Z0-9]{3,12}$/.test(value)) {
        callback(new Error("密码格式错误,应为3-12位的字母或数字"));
      } else {
        callback();
      }
    };
    var validateNewPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入新密码"));
      } else if (!/^[a-zA-Z0-9]{3,12}$/.test(value)) {
        callback(new Error("密码格式错误,应为3-12位的字母或数字"));
      } else if (value == this.modifyPasswordFrom.oldPassword) {
        callback(new Error("新密码不能和旧密码相同"));
      } else {
        if (this.modifyPasswordFrom.confirmPassword) {
          this.$refs.modifyPasswordFrom.validateField("confirmPassword");
        }
        callback();
      }
    };

    var validateconfirmPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入确认密码"));
      } else if (value !== this.modifyPasswordFrom.newPassword) {
        callback(new Error("两次密码不一致！"));
      } else {
        callback();
      }
    };
    return {
      modifyPasswordFrom: {
        oldPassword: "",
        newPassword: "",
        confirmPassword: "",
      },
      passwordInputType: "password",
      passwordInputType2: "password",
      rules: {
        oldPassword: [{ validator: validateOldPassword, trigger: "blur" }],
        newPassword: [{ validator: validateNewPassword, trigger: "blur" }],
        confirmPassword: [
          { validator: validateconfirmPassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.modifyPasswordFrom.validate(async (valid) => {
        if (valid) {
          // let res = await validateOldPwd({ newPwd: this.modifyPasswordFrom.oldPassword })
          // console.log(this.modifyPasswordFrom.oldPassword)
          // console.log(res.data)
          //   let res2 = await updatePwd({ newPwd: this.modifyPasswordFrom.newPassword })
          // console.log(res2, this.newPwd)
        } else {
          console.log("验证失败");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.modifyPasswordFrom.resetFields();
    },
    changeType() {
      if (this.passwordInputType == "password") {
        this.passwordInputType = "text";
      } else {
        this.passwordInputType = "password";
      }
    },
    changeType2() {
      if (this.passwordInputType2 == "password") {
        this.passwordInputType2 = "text";
      } else {
        this.passwordInputType2 = "password";
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input__inner {
  width: 230px;
}
.el-input {
  width: 230px;
}
</style>
